type Callback = () => void

type Options = {
  pageSize?: number
  pageSizeList?: number[]
}

const defaultPageSize = 10
const defaultPageSizeList = [10, 20, 50]

export function usePagination(callback: Callback, options: Options = { pageSize: 10, pageSizeList: [10, 20, 50] }) {
  const pagination = reactive({
    showPageSize: true,
    showTotal: true,
    pageNum: 1,
    pageSize: options.pageSize || defaultPageSize,
    total: 0,
    pageSizeList: options.pageSizeList || defaultPageSizeList,

    /** 处理分页操作 */
    onChange: (pageNum: number) => {
      pagination.pageNum = pageNum
      callback && callback()
    },
    onPageSizeChange: (pageSize: number) => {
      pagination.pageNum = 1
      pagination.pageSize = pageSize
      callback && callback()
    }
  })

  const onChangeCurrent = pagination.onChange
  const onChangePageSize = pagination.onPageSizeChange

  const setTotal = (total: number) => {
    pagination.total = total
  }

  const { pageNum, pageSize, total } = toRefs(pagination)

  return {
    pageNum,
    pageSize,
    total,
    pagination,
    onChangeCurrent,
    onChangePageSize,
    setTotal
  }
}
